package com.zdya.combatdocuments.controller;

import cn.hutool.core.util.StrUtil;
import com.zdya.combatdocuments.entity.DocApprovalLimit;
import com.zdya.combatdocuments.entity.Grade;
import com.zdya.combatdocuments.entity.vo.*;
import com.zdya.combatdocuments.p2pNetwork.NodeUtil;
import com.zdya.combatdocuments.service.SystemInfoService;
import com.zdya.combatdocuments.util.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * * @author: bj
 * * @date: 2023/6/9
 * * @desc: 系统操作表
 */
@RestController
@RequestMapping("/documents/system")
public class SystemInfoController {

    @Autowired
    private SystemInfoService systemInfoService;
    @Autowired
    NodeUtil nodeUtil;


    /**
     * 解析pdf文档签名信息
     * @param multipartFile
     * @return
     */
    @PostMapping("/readPdfInfo")
    public ResultVo readPdfInfo(@RequestParam("file") MultipartFile multipartFile) {
        String readPdfInfo = nodeUtil.readPdfInfo(multipartFile);
        if (StrUtil.isBlank(readPdfInfo)) {
            return ResultVoUtil.error("文件未检出鉴权信息");
        }
        return ResultVoUtil.success(readPdfInfo);
    }


    /**
     * 清除数据
     */
    @GetMapping("/clearData")
    public ResultVo clearData() {
        ResultVo resultVo = systemInfoService.clearData();
        nodeUtil.isClear=true;
        return resultVo;
    }

    /**
     * 查询文书限制设置信息
     * @return
     */
    @GetMapping("/queryDocLimit")
    public ResultVo queryDocLimit() {
        List<DocApprovalLimit> docApprovalLimits = systemInfoService.queryDocLimit();
        return ResultVoUtil.success(docApprovalLimits);
    }

    /**
     * 更新文书等级限制
     * @param docApprovalLimitVO
     * @return
     */
    @PostMapping("/updateDocLimit")
    public ResultVo updateDocLimit(@RequestBody DocApprovalLimitVO docApprovalLimitVO) {
        systemInfoService.updateDocLimit(docApprovalLimitVO);
        return ResultVoUtil.success(ResultEnum.SUCCESS.getMessage());
    }

    /**
     * 消息广播
     * @param broadcastVO
     * @return
     */
    @PostMapping("/broadcast")
    public ResultVo broadcast(@RequestBody BroadcastVO broadcastVO) {
        systemInfoService.broadcast(broadcastVO);
        return ResultVoUtil.success(ResultEnum.SUCCESS.getMessage());
    }

    /**
     * 查询广播消息列表
     * @return
     */
    @GetMapping("/queryBroadcasts")
    public ResultVo queryBroadcasts() {
        return ResultVoUtil.success(systemInfoService.queryBroadcasts());
    }

    /**
     * 查询当前网络阶段
     * @return
     */
    @GetMapping("/queryNetGrade")
    public ResultVo queryNetGrade() {
        return ResultVoUtil.success(systemInfoService.queryNetGrade());
    }

    /**
     * 更新网络阶段
     */
    @PostMapping("/updateNetGrade")
    public ResultVo updateNetGrade(@RequestBody GradeVO gradeVO) {
        ResultVo resultVo = systemInfoService.updateNetGrade(gradeVO);
        return resultVo;
    }


}
